﻿using System;
using System.Collections.Generic;
using System.Data;

namespace BrandonHaynes.Data
	{
	public abstract class TypedDataReaderAdapterBase<T> : TypedDataRecordAdapterBase<T>, IDataReader
		{
		private IEnumerable<T> Adaptees { get; set; }
		private IEnumerator<T> Enumerator { get; set; }

		protected TypedDataReaderAdapterBase(IEnumerable<T> adaptees)
			{
			Adaptees = new List<T>(adaptees);
			Enumerator = Adaptees.GetEnumerator();
			}

		#region IDataReader Members

		public void Close()
			{ Adaptees = null; }

		public int Depth { get; set; }

		public DataTable GetSchemaTable()
			{ return null; }

		public bool IsClosed 
			{ get { return Adaptees == null; } }

		public bool NextResult()
			{ return false; }

		public bool Read()
			{ 
			if(Enumerator.MoveNext())
				{
				Adaptee = Enumerator.Current;
				return true;
				}

			return false;
			}

		public int RecordsAffected
			{ get { return 0; } }

		#endregion

		#region IDisposable Members

		public void Dispose()
			{
			Dispose(true);
			GC.SuppressFinalize(this); 
			}

	   protected virtual void Dispose(bool disposing) 
			{
			if (disposing) 
				{
				var disposable = Adaptees as IDisposable;

				if (disposable != null)
					disposable.Dispose();
				}

			Adaptees = null;
			Adaptee = default(T);
			}

		#endregion
		}
	}
